<?php
namespace app\admin\controller;

use app\admin\model\Activity as ActivityModel;

class Activity extends Base {

    public $cModel = '';
    private static $user_activity = 'user_activity';

    public function _initialize() {
        parent::_initialize();
        $this->cModel = new ActivityModel();
    }

    public function index(){
        $params = input('get.');
        $where = [];
        $search = trim(input('get.search'));
        $search && $where['title'] = ['LIKE', "%$search%"];
        $list = $this->cModel->where($where)->order('id desc')->paginate('',false,page_param());
        $this->assign('data', $list);
        return $this->fetch();
    }

    public function edit(){
        if(request()->isPost()){
            $data = input('post.');
            $result = $this->validate($data, 'Activity');
            if(true !== $result){
                return $this->ajaxReturn(1,$result);
            }
            if($data['id']){
                $res = $this->cModel->allowField(true)->save($data, ['id'=>$data['id']]);
            }else{
                $this->cModel->images = $data['images'];
                $this->cModel->title = $data['title'];
                $this->cModel->content = $data['content'];
                $this->cModel->apply_start_time = $data['apply_start_time'];
                $this->cModel->apply_end_time = $data['apply_end_time'];
                $this->cModel->start_time = $data['start_time'];
                $this->cModel->end_time = $data['end_time'];
                $this->cModel->addr = $data['addr'];
                $this->cModel->num = $data['num'];
                $this->cModel->max_num = $data['max_num'];
                $this->cModel->save();
                $this->cModel->sign_out_token = md5(mt_rand(1000, 9999) . time());
                $str = json_encode(['type'=>1, 'date'=>$this->cModel->start_time, 'activity_id'=>$this->cModel->id]);
                $this->cModel->sign_in_qrcode = \app\api\logic\UserLogic::activity_qrcode($str);
                $str = json_encode(['type'=>2, 'date'=>$this->cModel->end_time, 'activity_id'=>$this->cModel->id, 'token'=>$this->cModel->sign_out_token]);
                $this->cModel->sign_out_qrcode = \app\api\logic\UserLogic::activity_qrcode($str);
                $res = $this->cModel->save();
            }
            if($res){
                return $this->ajaxReturn(0, "操作成功", url('index'));
            }else{
                return $this->ajaxReturn(1,"操作失败");
            }
        }
        $id = input('id');
        $id ? $where['id']=$id : $where['id']=0;
        $info = $this->cModel->where($where)->find();
        if(input('qrcode')){
            return json(['code'=>$info['sign_out_qrcode']]);
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    //删除用户
    public function delete(){
        $params =   [
            "id"    =>  input("post.id/d",0),
        ];
        $this->cModel->where($params)->delete();
        return $this->ajaxReturn();
    }

    // 用户活动报名列表
    public function user_activity_index(){
        $get_data = input('get.');
        $where = [];
        if(isset($get_data['search'])){
            $search=trim($get_data['search']);
            $search && $where['ua.name|a.title']=['LIKE', "%$search%"];
        }
        if(isset($get_data['start_time']) && $get_data['start_time']){
            $get_data['start_time'] = strtotime($get_data['start_time']);
            $get_data['end_time'] ? $get_data['end_time']=strtotime($get_data['end_time'])+86400 : $get_data['end_time'] = time()+86400;
            $where['ua.create_time'] = [['>=',$get_data['start_time']], ['<=',$get_data['end_time']], 'AND'];
        }
        $field = [
            'ua.id', 'ua.create_time', 'ua.name', 'ua.sex', 'ua.birthday', 'ua.mobile',
            'ua.is_sign_on', 'ua.is_sign_off', 'ua.sign_on_time', 'ua.sign_off_time', 'a.title'
            ];
        $list = model(self::$user_activity)->alias('ua')
            ->join('activity a', 'ua.activity_id=a.id')->field($field)
            ->where($where)->order('ua.id desc')->paginate('',false,page_param());
        $this->assign('data', $list);
        return $this->fetch();
    }

    public function user_activity_edit(){
        $id = input('id');
        $id ? $where['ua.id']=$id : $where['ua.id']=0;
        $field = [
            'ua.*', 'a.title', 'u.nickname'
        ];
        $info = model(self::$user_activity)->alias('ua')
            ->join('activity a', 'a.id=ua.activity_id')->join('users u', 'u.id=ua.user_id')
            ->where($where)->field($field)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function user_activity_del(){
        if(request()->isPost()){
            $data = input('post.');
            $res = model(self::$user_activity)->where('id', $data['id'])->delete();
            $res ? $msg = '操作成功' : $msg = '操作失败';
            return $this->ajaxReturn(0, $msg, url('user_activity_index'));
        }
    }

    /**
     * 积分排行
     * @return mixed
     */
    public function integral_rankings(){
        $params = input('');
        if(isset($params['show_points']) && isset($params['user_id']) && $params['show_points'] && $params['user_id']){
            $nickname = model('ClientUsers')->where(['id'=>$params['user_id']])->value('nickname');
            $this->redirect(url('client_user/user_points_log_index').'?search='.$nickname);
            exit();
        }
        $where = [];
        $search = trim(input('get.search'));
        $search && $where['nickname'] = ['LIKE', "%$search%"];
        $item_num = 15; //每页显示多少个
        $now_num = input('get.page',1) * $item_num - $item_num + 1; //第n页时的第一个名次
        $list = model('ClientUsers')->where($where)
            ->order('points desc')->paginate($item_num,false,page_param());
        $this->assign('data', $list);
        $this->assign('now_num', $now_num);
        return $this->fetch();
    }

}